﻿using EHR18.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common;

namespace EHR18
{
    public partial class FormDisplay : BaseChild
    {
        System.Timers.Timer timer = new System.Timers.Timer(1000);
        public FormDisplay()
        {
            InitializeComponent();
        }
        private static FormDisplay frm = null;
        public static FormDisplay getInstance()
        {
            if (frm == null || frm.IsDisposed)
            {
                frm = new FormDisplay();
            }
            return frm;
        }

        private void FormDisplay_Load(object sender, EventArgs e)
        {
            var machine = ProductMachine.inst;
            machine.ShowTestSpan += inst_ShowTestSpan;
            //       UserData.GetInstance().ChangeProduct();
            timer.Elapsed += Timer_Elapsed;
            timer.Start();

            if (!PLCDevice.instance.IsOpen)
            {
                MessageBox.Show("PLC串口未打开");
            }
            else
            {
                UserData.GetInstance().ChangeProduct();
            }
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //   throw new NotImplementedException();

            try
            {

                string exportTime = GenneralConfig.instance.ExportXlsxTime;
                if (DateTime.Now.ToString("HH:mm:ss") == exportTime)
                {
                    TBAllService service = new TBAllService(DateTime.Now.AddDays(-1), DateTime.Now);
                    ExcelReport.Saveexcel(service, service.GetTable(GenneralConfig.instance.module.NameMappings), service.dtFrom, service.dtTo, "template.xlsx",
                        System.IO.Path.Combine(GenneralConfig.instance.OutputPath, "Excel Report", GenneralConfig.instance.MachineNo + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx"));
                }
            }
            catch (Exception ex)
            {
                ShowError("保存失败" + Environment.NewLine + ex.StackTrace);
            }
        }

        void inst_ShowTestSpan(List<TestSpan> obj)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                this.dataGridView1.DataSource = new BindingList<TestSpan>(obj);
                var counter = CountManager.getCount("总体结果");
                if (counter.Total > 0)
                {
                    this.cTextBox5.Value = counter.Total.ToString();
                    this.cTextBox6.Value = counter.OK.ToString();
                    this.cTextBox7.Value = counter.PassRate;
                    this.cTextBox8.Value = counter.NG.ToString();
                    this.cTextBox9.Value = counter.NgRate;
                }
            }));

        }

        public void UpdateTotalStatistics()
        {
            var counter = CountManager.getCount("总体结果");
            if (counter.Total > 0)
            {
                this.cTextBox5.Value = counter.Total.ToString();
                this.cTextBox6.Value = counter.OK.ToString();
                this.cTextBox7.Value = counter.PassRate;
                this.cTextBox8.Value = counter.NG.ToString();
                this.cTextBox9.Value = counter.NgRate;
            }
            else
            {
                this.cTextBox5.Value = "0";
                this.cTextBox6.Value = "0";
                this.cTextBox7.Value = "100%";
                this.cTextBox8.Value = "0";
                this.cTextBox9.Value = "0";
            }
            this.dataGridView1.DataSource = new BindingList<TestSpan>(new List<TestSpan>());
        }

        private void btntest_Click(object sender, EventArgs e)
        {
            ProductMachine.inst.Step();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            cTextBox1.Value = GenneralConfig.instance.MO;
            cTextBox2.Value = DateTime.Today.ToString("yyyy-MM-dd");
            cTextBox4.Value = GenneralConfig.instance.CurrentModel;
            cTextBox3.Value = GenneralConfig.instance.OperID;

            this.cTextBox10.Value = ProductMachine.inst.Products.Count.ToString();
        }

        private void btntest_Click_1(object sender, EventArgs e)
        {
            ProductMachine.inst.Step();
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView dataGridView = sender as DataGridView;
            if (dataGridView.Columns[e.ColumnIndex].HeaderText == "测试结果")
            {
                if (e.Value.ToString() == "PASS")
                {
                    e.CellStyle.BackColor = Color.LimeGreen;
                }
                else if (e.Value.ToString() == "FAIL")
                {
                    e.CellStyle.BackColor = Color.Red;
                }
            }
        }
    }
}
